package com.yb.livewykt.ui.activity

import android.content.Intent
import android.util.Log
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.observe
import com.yb.livewykt.R
import com.yb.livewykt.databinding.ActivityLoginLayoutBinding
import com.yb.livewykt.util.SaveUserData
import com.yb.livewykt.util.showToast
import com.yb.livewykt.vm.LoginViewModel

class LoginActivity : BaseActivity() {

    val viewModel by viewModels<LoginViewModel>()
    lateinit var binding:ActivityLoginLayoutBinding


    override fun initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login_layout)
        binding.data = viewModel
        binding.lifecycleOwner = this
    }

    override fun initListener() {
        viewModel.myData.observe(this ){
            Log.d(TAG, "initListener: $it")
            SaveUserData.get().accId =  it.accid
            SaveUserData.get().token =  it.token
            SaveUserData.get().username =  it.name+""
            SaveUserData.get().id =  it.id
            SaveUserData.get().imToken =  it.imToken
            //SaveUserData.get().qrCodeUrl =  it.qrCodeUrl
            SaveUserData.get().role =  it.role
            Log.d(TAG, "initListener: "+it.imToken+"\t"+it.accid)
            viewModel.wyLogin(it.accid,it.imToken)
        }

        viewModel.isLoginSuccess.observe(this){
            if (it){
                resources.getString(R.string.login_success).showToast()
                startActivity(Intent(this,LiveStreamingActivity::class.java))
            }else{
                resources.getString(R.string.login_failed).showToast()
            }
        }


        viewModel.showErrorMsg.observe(this){
            it.msg.showToast()
        }
    }

}